package com.watayouxiang.androidutils.engine;

import android.view.View;

import androidx.annotation.NonNull;

import com.maning.imagebrowserlibrary.MNImageBrowser;
import com.watayouxiang.androidutils.R;

import java.util.ArrayList;

/**
 * <pre>
 *     author : TaoWang
 *     e-mail : watayouxiang@qq.com
 *     time   : 2020/08/17
 *     desc   :
 * </pre>
 */
public abstract class MNImageBrowserUtils {

    /**
     * 获取资源地址
     */
    public abstract String getResUrl(String imgUrl);

    public void showPic(@NonNull View view, int currentPosition, @NonNull String... imgUrls) {
        /* 处理数据 */
        String imgUrl = null;
        ArrayList<String> imgUrlList = null;
        if (imgUrls.length == 0) {
            return;
        } else if (imgUrls.length == 1) {
            imgUrl = getResUrl(imgUrls[0]);
            currentPosition = 0;
        } else {
            imgUrlList = new ArrayList<>();
            for (String url : imgUrls) {
                imgUrlList.add(getResUrl(url));
            }
            if (currentPosition < 0 || currentPosition > imgUrlList.size() - 1) {
                currentPosition = 0;
            }
        }

        /* 显示图片预览 */
        MNImageBrowser imageBrowser = MNImageBrowser.with(view.getContext())
                // 图片加载器
                .setImageEngine(new MNImageBrowserEngine())
                // 手势下拉缩小效果是否开启
                .setOpenPullDownGestureEffect(true)
                // 自定义ProgressView，不设置默认默认没有
                .setCustomProgressViewLayoutID(R.layout.pic_viewer_progress)
                // 设置当前位置
                .setCurrentPosition(currentPosition);

        if (imgUrl != null) {
            // 必须（setImageList和setImageUrl二选一，会覆盖）-设置单张图片
            imageBrowser.setImageUrl(getResUrl(imgUrl));
        } else if (imgUrlList != null) {
            // 必须（setImageList和setImageUrl二选一，会覆盖）-图片集合
            imageBrowser.setImageList(imgUrlList);
        }

        imageBrowser.show(view);
    }

    public void showPic(@NonNull View view, @NonNull String imgUrl) {
        showPic(view, 0, imgUrl);
    }

    public void clickViewShowPic(@NonNull View view, int currentPosition, @NonNull String... imgUrls) {
        if (!view.isClickable()) {
            view.setClickable(true);
        }
        view.setOnClickListener(v -> showPic(v, currentPosition, imgUrls));
    }

    public void clickViewShowPic(@NonNull View view, @NonNull String imgUrl) {
        clickViewShowPic(view, 0, imgUrl);
    }

}
